<template>
  <van-icon @click="onLike" :color="value === 1 ? '#f64f59' : ''" :name="value === 1 ? 'good-job' : 'good-job-o'" />
</template>

<script>
import { addLike, deleteLike } from '@/api/article'
export default {
  name: 'LikeArticle',
  props: {
    article_id: {
      type: [Number, String, Object],
      required: true
    },
    value: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
    }
  },
  methods: {
    async onLike () {
      try {
        if (this.value === 1) {
          // 取消点赞
          await deleteLike(this.article_id)
        } else {
          // 点赞
          await addLike(this.article_id)
        }
        this.$toast.success(this.value === 1 ? '取消点赞' : '已点赞')
        this.$emit('input', this.value === 1 ? -1 : 1)
      } catch (err) {
        this.$toast.fail('操作失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
</style>
